﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpGradientHeightParameterUpdater.h"
#include "DbObjectUtils.h"
#include "GbmpBuiltInParameterDefinitionsNew.h"
#include "IElement.h"
#include "IElementParameters.h"
#include "IElementPosition.h"
#include "IParameter.h"
#include "IParameterDefinitionLibrary.h"
#include "IPositionTwoAssociatedPlanesWithFourOffsets.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"



using namespace gcmp;
DBOBJECT_DATA_DEFINE(GbmpGradientHeightParameterUpdater)
{
}

gcmp::OwnerPtr<gcmp::IInstanceFamilyParametersUpdater> gcmp::GbmpGradientHeightParameterUpdater::Clone() const
{
    return NEW_AS_OWNER_PTR(GbmpGradientHeightParameterUpdater, *this);
}

bool gcmp::GbmpGradientHeightParameterUpdater::UpdateFamilyParametersToPositionParameters(IElement *pElement)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement != nullptr, L"无效pElement",L"GDMPLab",L"2024-03-30");
    IElementPosition *posBehavior = pElement->GetElementPosition();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(posBehavior != nullptr, L"无效posBehavior",L"GDMPLab",L"2024-03-30");
    const IElementParameters* pParameters = pElement->GetElementParameters();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pParameters != nullptr, L"无效pParameters",L"GDMPLab",L"2024-03-30");

    double maxHeight = 0;
    bool hasParameter = pParameters->GetParameterValueAsDouble(PARAMETER_UID(VariableSectionWallMaxHeightBuiltInParameter), maxHeight);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(hasParameter, L"没有高度参数",L"GDMPLab",L"2024-03-30");

    double startBottomOffset = 0;
    hasParameter = pParameters->GetParameterValueAsDouble(PARAMETER_UID(WallStartBottomOffsetBuiltInParameter), startBottomOffset);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(hasParameter, L"没有起点偏移参数",L"GDMPLab",L"2024-03-30");

    double endBottomOffset = 0;
    hasParameter = pParameters->GetParameterValueAsDouble(PARAMETER_UID(WallEndBottomOffsetBuiltInParameter), endBottomOffset);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(hasParameter, L"没有终点偏移参数",L"GDMPLab",L"2024-03-30");

    double startWallHeight = 0;
    hasParameter = pParameters->GetParameterValueAsDouble(PARAMETER_UID(WallStartHeightBuiltInParameter), startWallHeight);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(hasParameter, L"没有起点高度参数",L"GDMPLab",L"2024-03-30");

    double endWallHeight = 0;
    hasParameter = pParameters->GetParameterValueAsDouble(PARAMETER_UID(WallEndHeightBuiltInParameter), endWallHeight);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(hasParameter, L"没有终点高度参数",L"GDMPLab",L"2024-03-30");
    
    IPositionTwoAssociatedPlanesWithFourOffsets *pAssocicatedPlanes = dynamic_cast<IPositionTwoAssociatedPlanesWithFourOffsets *>(posBehavior->GetPositionAssociatedPlane());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pAssocicatedPlanes, L"无效pAssocicatedPlanes",L"GDMPLab",L"2024-03-30");
    return pAssocicatedPlanes->UpdateGradientHeightInfo(maxHeight
        , startWallHeight
        , endWallHeight
        , startBottomOffset
        , endBottomOffset
        );
}

bool gcmp::GbmpGradientHeightParameterUpdater::UpdatePositionParametersToFamilyParameters(IElement *pElement)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement != nullptr, L"无效pElement",L"GDMPLab",L"2024-03-30");
    const IElementPosition *posBehavior = pElement->GetElementPosition();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(posBehavior != nullptr, L"无效posBehavior",L"GDMPLab",L"2024-03-30");
    const IPositionTwoAssociatedPlanesWithFourOffsets *pAssocicatedPlanes = dynamic_cast<const IPositionTwoAssociatedPlanesWithFourOffsets *>(posBehavior->GetPositionAssociatedPlane());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pAssocicatedPlanes, L"无效pAssocicatedPlanes",L"GDMPLab",L"2024-03-30");

    double maxHeight = 0;
    double startHeight = 0;
    double endHeight = 0;
    double familyStartBottomOffset = 0;
    double familyEndBottomOffset = 0;
    bool isSuccess = pAssocicatedPlanes->GetGradientHeightInfo(maxHeight,startHeight,endHeight,familyStartBottomOffset,familyEndBottomOffset);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(isSuccess, L"无法读取高度参数",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IParameter> maxHeightParam = ELEMENT_PARAMETER(pElement, VariableSectionWallMaxHeightBuiltInParameter);
    OwnerPtr<IParameter> startBottomOffsetParam = ELEMENT_PARAMETER(pElement, WallStartBottomOffsetBuiltInParameter);
    OwnerPtr<IParameter> endBottomOffsetParam = ELEMENT_PARAMETER(pElement, WallEndBottomOffsetBuiltInParameter);
    OwnerPtr<IParameter> startWallHeightParam = ELEMENT_PARAMETER(pElement, WallStartHeightBuiltInParameter);
    OwnerPtr<IParameter> endWallHeightParam = ELEMENT_PARAMETER(pElement, WallEndHeightBuiltInParameter);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(maxHeightParam, L"没有高度参数",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(startBottomOffsetParam, L"没有起点偏移参数",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(endBottomOffsetParam, L"没有终点偏移参数",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(startWallHeightParam, L"没有起点高度参数",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(endWallHeightParam, L"没有终点高度参数",L"GDMPLab",L"2024-03-30");
    maxHeightParam->SetValueAsDouble(maxHeight);
    if (!pElement->GetElementParameters())
        return false;
    pElement->GetElementParameters()->SetParameter(maxHeightParam.get());

    startWallHeightParam->SetValueAsDouble(startHeight);
    pElement->GetElementParameters()->SetParameter(startWallHeightParam.get());
    endWallHeightParam->SetValueAsDouble(endHeight);
    pElement->GetElementParameters()->SetParameter(endWallHeightParam.get());

    startBottomOffsetParam->SetValueAsDouble(familyStartBottomOffset);
    pElement->GetElementParameters()->SetParameter(startBottomOffsetParam.get());
    endBottomOffsetParam->SetValueAsDouble(familyEndBottomOffset);
    pElement->GetElementParameters()->SetParameter(endBottomOffsetParam.get());
    return true;
}
